package com.hrprojectback.controller.XinXiGuanLi;

import com.alibaba.fastjson.JSON;
import com.hrprojectback.dto.*;
import com.hrprojectback.enums.StatusEnum;
import com.hrprojectback.service.XinXiGuanLiService.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private Department department;

    /**
     * 查询部门接口
     *
     * @param queryInfo
     * @return
     */
    @GetMapping("/department")
    public ResponseDTO getDepartment(QueryInfo queryInfo) {
        log.info("查询部门数据===============");
        //获取部门的总数量
        Integer deptCounts = departmentService.getDeptCounts(queryInfo.getCode(), queryInfo.getName());
        log.info("查询部门总数量为：" + deptCounts);
        //开始页
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        List<Department> departments = departmentService.getDepartment(queryInfo.getCode(), queryInfo.getName(), pageStart, queryInfo.getPageSize());
        if (departments.size() == 0) {
            return Response.error2(StatusEnum.STAFF_NOT_EXIST3.getCode(), StatusEnum.STAFF_NOT_EXIST3.getMessage(), departments);
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("deptCounts", deptCounts);
        res.put("data", departments);
        String BuMen = JSON.toJSONString(res);
        log.info("查询结束=========");
        return Response.success(res);
    }

    @GetMapping("/deptPCodeList")
    public ResponseDTO getDeptPCodeTreeView(DepertmentPCode depertmentPCode) {
        log.info("设置上级部门code===============");
        if (!StringUtils.hasText(depertmentPCode.getPcode()) & !StringUtils.hasText(depertmentPCode.getCode())) {
            depertmentPCode.setPcode("-1");
        }
        List<DepertmentPCode> departments = getDepartmentPCodeList(depertmentPCode);
        if (departments.size() == 0) {
            return Response.error2(StatusEnum.STAFF_NOT_EXIST3.getCode(), StatusEnum.STAFF_NOT_EXIST3.getMessage(), departments);
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("data", departments);
        log.info("查询结束=========");
        return Response.success(res);
    }

    /**
     * 递归查询部门
     *
     * @param depertmentPCode
     * @return
     */
    private List<DepertmentPCode> getDepartmentPCodeList(DepertmentPCode depertmentPCode) {
        //开始页
        int pageStart = (department.getPageNum() - 1) * department.getPageSize();
        List<DepertmentPCode> departments1 = departmentService.selectDepartmentPCodeTrees(depertmentPCode);
        for (int i = 0; i < departments1.size(); i++) {
            if (!StringUtils.hasText(departments1.get(i).getCode())) {
                continue;
            }
            DepertmentPCode departments2 = new DepertmentPCode();
            departments2.setPcode(departments1.get(i).getCode());
            log.info("departments2" + departments2);
            List<DepertmentPCode> dtos = getDepartmentPCodeList(departments2);
            departments1.get(i).setChildDept(dtos);
        }
        return departments1;
    }

    /**
     * 新增部门接口
     *
     * @param department
     * @return
     */
    @PostMapping("/addDept")
    public ResponseDTO addDept(@RequestBody Department department) {
        log.info("添加部门=========");
        //默认是生效
        department.setStatus("1");
        int i = departmentService.addDept(department);

        if (i > 0) {
            log.info("新增部门成功=========");
            return Response.success(department);
        }
        return Response.error();
    }

    /**
     * 根据部门code去查询需要编辑的部门信息
     *
     * @param code
     * @return
     */
    @RequestMapping("/getEditDepartment")
    public String editDepartment(String code) {
        log.info("查询编辑的部门信息=========");
        Department geteditDept = departmentService.geteditDepartment(code);
        String string = JSON.toJSONString(geteditDept);
        return string;
    }

    /**
     * 编辑部门信息
     *
     * @param department
     * @return
     */
    @PutMapping("/editDepartment")
    public ResponseDTO editDept(@RequestBody Department department) {
        log.info("对单个部门进行编辑=========");
        int i = departmentService.editDept(department);
        if (i > 0) {
            return Response.success(department);
        }
        return Response.error();
    }

    /**
     * 根据部门code删除部门
     *
     * @param code
     * @return
     */
    @DeleteMapping("/deleteDepartment")
    public ResponseDTO deleteDept(String code) {
        log.info("对单个部门进行删除=========");
        int i = departmentService.deleteDept(code);
        if (i > 0) {
            log.info("删除部门成功=========");
            return Response.success();
        }
        return Response.error();
    }

    /**
     * 根据部门code生效部门
     *
     * @param code
     * @param status
     * @return
     */
    @RequestMapping("/deptStateShengXiao")
    public ResponseDTO deptStateQiYong(@RequestParam("code") String code, @RequestParam("status") int status) {
        log.info("对单个部门进行生效=========");
        if (status == 1) {
            return Response.error(StatusEnum.STAFF_IS_DEPT_SHENG_XIAO);
        }
        int qiYong = departmentService.YiQiYong(code);
        if (qiYong > 0) {
            return Response.successStatus(qiYong);
        }
        return Response.error(StatusEnum.STATUS_SHENG_XIAO_ERROR);
    }

    /**
     * 根据部门code失效部门
     *
     * @param code
     * @param status
     * @return
     */
    @RequestMapping("/deptStateShiXiAo")
    public ResponseDTO deptStateShiXiAo(@RequestParam("code") String code, @RequestParam("status") int status) {
        log.info("对单个部门进行失效=========");
        if (status == 0) {
            return Response.error(StatusEnum.STAFF_IS_DEPT_SHI_XIAO);
        }
        int shiXiAo = departmentService.YiShiXiAo(code);
        if (shiXiAo > 0) {
            return Response.successStatus2(shiXiAo);
        }
        return Response.error(StatusEnum.STATUS_SHI_XIAO_ERROR);
    }

    /**
     * 根据部门code批量删除部门
     *
     * @param codes
     * @return
     */
    @DeleteMapping("/BatchDeleteDept")
    public ResponseDTO BatchDeleteDept(@RequestBody List<String> codes) {
        log.info("对部门进行批量删除=========");
        if (codes.size() > 0) {
            for (String code : codes) {
                deleteDept(code);
            }
            return Response.success();
        }
        return Response.error();
    }

    /**
     * 通过部门code批量生效部门
     *
     * @param codes
     * @return
     */
    @RequestMapping("/BatchShengXiaoDept")
    public ResponseDTO BatchShengXiaoUser(@RequestBody List<String> codes) {
        log.info("对部门进行批量生效=========");
        if (codes.size() == 0) {
            return Response.success();
        }
        if (codes.size() > 0) {
            ArrayList statusList = new ArrayList();
            for (String code : codes) {
                //查看部门的当前状态
                int deptStatus = departmentService.getStatus(code);
                if (deptStatus == 1) {
                    statusList.add(deptStatus);
                }
            }
            if (statusList.size() > 0) {
                return Response.error(StatusEnum.SHENG_XIAO_STAFF_IS_EXIST);
            }
            for (String code : codes) {
                departmentService.YiQiYong(code);
            }
            return Response.success();
        }
        return Response.error();
    }

    /**
     * 通过部门code批量失效部门
     *
     * @param codes
     * @return
     */
    @RequestMapping("/BatchShiXiaoDept")
    public ResponseDTO BatchShiXiaoUser(@RequestBody List<String> codes) {
        log.info("对部门进行批量失效=========");
        if (codes.size() == 0) {
            return Response.success();
        }
        if (codes.size() > 0) {
            ArrayList statusList = new ArrayList();
            for (String code : codes) {
                //查看部门的当前状态
                int deptStatus = departmentService.getStatus(code);
                if (deptStatus == 0) {
                    statusList.add(deptStatus);
                }
            }
            if (statusList.size() > 0) {
                return Response.error(StatusEnum.SHI_XIAO_STAFF_IS_EXIST);
            }
            for (String code : codes) {
                departmentService.YiShiXiAo(code);
            }
            return Response.success();
        }
        return Response.error();
    }
}
